from .views import *

# 下单
class Place(APIView):
    def get(self, request):
        res = {}
        order = request.GET.get('order')
        o = Order.objects.filter(order= order).first()
        if not o.result == '未开启':
            res['code'] = 403
            res['message'] = '该订单已经下单'
            return Response(res)
        o.result = '在途'
        o.start_time = timezone.now()
        o.save()
        res['code'] = 200
        res['message'] = '下单成功'
        return Response(res)


# 收货
class Receive(APIView):
    def get(self, request):
        res = {}
        order = request.GET.get('order')
        o = Order.objects.filter(order= order).first()
        try:
            res['data'] = {}
            res['data']['log'] = resultLog(o)[0]
            res['data']['alert'] = resultLog(o)[1]
            res['data']['over'] = resultLog(o)[2]
            o.report = json.dumps(res['data'], ensure_ascii=False)
            o.save()
            res['code'] = 200
        except:
            res['code'] = 403
            res['message'] = '请求失败'

        return Response(res)

# 校验
class CheckOrder(APIView):
    def get(self, request):
        res = {}
        o = Order.objects.filter(order= request.GET.get('order')).first()
        if not o.result == '在途':
            paramsError(res)
        o.result = request.GET.get('oprate')
        o.end_time = timezone.now()
        o.save()
        res['code'] = 200
        res['message'] = '操作成功'
        return Response(res)